package Client;
import java.io.IOException;

public final class Packets {
    private static final int[] PACKET_SIZES = {
        0, -2, 4
    };

    public static void readIncomingPackets() {
        try {
            ByteStream inStream = GameCanvas.inStream();
            for (int i = 0; i < 50; i++) {
                int avail = GameCanvas.in().available();
                if (avail == 0) {
                    break;
                }
                inStream.setOffset(0);
                inStream.setLength(avail);
                GameCanvas.in().read(inStream.buffer(), 0, 1);
                int opcode = inStream.getOpcode();
                int size = PACKET_SIZES[opcode];
                if (size == -1) {
                    GameCanvas.in().read(inStream.buffer(), 1, 1);
                    size = inStream.getUnsignedByte();
                } else if (size == -2) {
                    GameCanvas.in().read(inStream.buffer(), 1, 2);
                    size = inStream.getUnsignedShort();
                }
                inStream.setOffset(0);
                GameCanvas.in().read(inStream.buffer(), 0, size);
                switch (opcode) {
                case 1:
                    int flag = inStream.getByte();
                    if (flag == 1) {
                        GameCanvas.myPlayer().setCoords(inStream.getUnsignedShort(), inStream.getUnsignedShort());
                    } else if (flag == 2) {
                        GameCanvas.myPlayer().move(inStream.getByte());
                    }
                    boolean hasUpdate = inStream.getUnsignedByte() == 1;
                    if (hasUpdate) {
                        int mask = inStream.getUnsignedByte();
                        if ((mask & 0x1) != 0) {
                            GameCanvas.myPlayer().doChat(inStream.getString());
                            GameCanvas.addChatboxMessage("@whi@" + GameCanvas.myPlayer().username() + ": " +  GameCanvas.myPlayer().chat());
                        }
                        if ((mask & 0x2) != 0) {
                            GameCanvas.myPlayer().setAppearance(inStream.getUnsignedByte(), inStream.getUnsignedByte(), inStream.getUnsignedByte());
                            GameCanvas.myPlayer().buildAppearance();
                        }
                    }
                    for (int i2 = 0; i2 < GameCanvas.players().size(); i2++) {
                        Player p = GameCanvas.players().get(i2);
                        int flag2 = inStream.getByte();
                        if (flag2 == 1) {
                            GameCanvas.players().remove(i2--);
                        } else if (flag2 == 2) {
                            p.move(inStream.getByte());
                        }
                        hasUpdate = inStream.getUnsignedByte() == 1;
                        if (hasUpdate) {
                            int mask = inStream.getUnsignedByte();
                            if ((mask & 0x1) != 0) {
                                p.doChat(inStream.getString());
                                GameCanvas.addChatboxMessage("@whi@" + p.username() + ": " +  p.chat());
                            }
                            if ((mask & 0x2) != 0) {
                                p.setAppearance(inStream.getUnsignedByte(), inStream.getUnsignedByte(), inStream.getUnsignedByte());
                                p.buildAppearance();
                            }
                        }
                    }
                    int x;
                    while ((x = inStream.getByte()) != 127) {
                        Player p = new Player();
                        p.setCoords(GameCanvas.myPlayer().x() + x, GameCanvas.myPlayer().y() + inStream.getByte());
                        p.setUsername(inStream.getString());
                        GameCanvas.players().add(p);
                        int mask = inStream.getUnsignedByte();
                        if ((mask & 0x1) != 0) {
                            p.doChat(inStream.getString());
                            GameCanvas.addChatboxMessage("@whi@" + p.username() + ": " +  p.chat());
                        }
                        if ((mask & 0x2) != 0) {
                            p.setAppearance(inStream.getUnsignedByte(), inStream.getUnsignedByte(), inStream.getUnsignedByte());
                            p.buildAppearance();
                        }
                    }
                    break;
                case 2:
                    WorldController.loadMaps(inStream.getUnsignedShort(), inStream.getUnsignedShort());
                    break;
                }
            }
        } catch (IOException ioe) {
            GameCanvas.logout();
            ioe.printStackTrace();
        }
    }
}